import java.util.HashSet;
import java.util.Set;

public class Solution {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    //链表相交节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> set = new HashSet<>();
        ListNode cur = headA;
        //将A链表放入hashSet中
        while(cur != null) {
            set.add(cur);
            cur = cur.next;
        }
        //遍历链表B找到相同节点
        ListNode sort = null;
        cur = headB;
        while(cur != null) {
            if(set.contains(cur)) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
}
